package cn.iocoder.yudao.module.security.service.communicate;

import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.security.controller.admin.communicate.vo.*;
import cn.iocoder.yudao.module.security.dal.dataobject.communicate.SecurityCommunicateDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.security.dal.mysql.communicate.SecurityCommunicateMapper;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.security.enums.ErrorCodeConstants.*;

/**
 * 通信行为 Service 实现类
 *
 * @author cai
 */
@Service
@Validated
public class SecurityCommunicateServiceImpl implements SecurityCommunicateService {

    @Resource
    private SecurityCommunicateMapper communicateMapper;

    @Override
    public Long createCommunicate(SecurityCommunicateSaveReqVO createReqVO) {
        // 插入
        SecurityCommunicateDO communicate = BeanUtils.toBean(createReqVO, SecurityCommunicateDO.class);
        communicateMapper.insert(communicate);
        // 返回
        return communicate.getId();
    }

    @Override
    public void updateCommunicate(SecurityCommunicateSaveReqVO updateReqVO) {
        // 校验存在
        validateCommunicateExists(updateReqVO.getId());
        // 更新
        SecurityCommunicateDO updateObj = BeanUtils.toBean(updateReqVO, SecurityCommunicateDO.class);
        communicateMapper.updateById(updateObj);
    }

    @Override
    public void deleteCommunicate(Long id) {
        // 校验存在
        validateCommunicateExists(id);
        // 删除
        communicateMapper.deleteById(id);
    }

    private void validateCommunicateExists(Long id) {
        if (communicateMapper.selectById(id) == null) {
            throw exception(COMMUNICATE_NOT_EXISTS);
        }
    }

    @Override
    public SecurityCommunicateDO getCommunicate(Long id) {
        return communicateMapper.selectById(id);
    }

    @Override
    public PageResult<SecurityCommunicateDO> getCommunicatePage(SecurityCommunicatePageReqVO pageReqVO) {
        return communicateMapper.selectPage(pageReqVO);
    }

}